﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DALLayer.Providers;
using DataDefinition;
using Objects;

namespace BusinessLogicLayer.Manager
{
    public class CommentManager : ManagerBase<CommentDataProvider>
    {
         #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="CommentManager" /> class.
        /// </summary>
        public CommentManager() { } 
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new CommentDataProvider();
        }

        #endregion

        #region [Public Methods]
        /// <summary>
        /// Gets the comments by article id
        /// </summary>
        /// <param name="articleId">article id</param>
        /// <returns>comment details</returns>
        public List<GetCommentByArticleID> GetCommentByArticleID(int articleId)
        {
            //The article type details
            List<GetCommentByArticleID> commentDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = this.DataProvider.GetCommentByArticleID(articleId);
                if (dataSet != null && dataSet.GetCommentArticleID != null && dataSet.GetCommentArticleID.Rows != null)
                {
                    //Set the returned filled list
                    commentDetails = FillGetCommentByArticleIDObjectsByEntities(dataSet.GetCommentArticleID);
                }
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The comment details list to return
            return commentDetails;
        }
         /// <summary>
        /// Update or insert an comment.
        /// </summary>
        /// <param name="comment">comment object</param>
        /// <returns>true or false</returns>
        public bool UpdateComment(Comment comment)
        {
            bool success = false;
            try
            {
                //if there is an id
                if (comment.CommentID > 0)
                {
                    //Update the comment
                    success = base.DataProvider.UpdateComment(comment.CommentID, comment.CommentData, comment.UserID, comment.ArticleID, comment.IsActive);
                }
                else
                {
                    //Insert an comment
                    success = base.DataProvider.InsertComment(comment.CommentData, comment.UserID, comment.ArticleID, comment.IsActive);

                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns true or false
            return success;

        }
        #endregion

        #region [Private Methods]

        /// <summary>
        /// Fills the article comment object
        /// </summary>
        /// <param name="dataTable">the data table</param>
        /// <returns>The article comment list</returns>
        private List<GetCommentByArticleID> FillGetCommentByArticleIDObjectsByEntities(AutoFixDataSet.GetCommentArticleIDDataTable dataTable)
        {
            //The comment list that contains the objects
            //Loop through the data table and create new ArticleCommentObject 
            //finally select the ArticleCommentObject and convert to list
            List<GetCommentByArticleID> articleCommentDetails = (from acdo in
                                                                     (from acd in dataTable.AsEnumerable<AutoFixDataSet.GetCommentArticleIDRow>()
                                                                      select new
                                                                      {
                                                                          ArticleCommentObject = new GetCommentByArticleID()
                                                                          {
                                                                              CommentText = acd.Comment,
                                                                              UserName = acd.UserName,
                                                                              ProfilePictureURL = acd.ProfilePictureURL,
                                                                              CommentID = acd.CommentID
                                                                          }
                                                                      })
                                                                 select acdo.ArticleCommentObject).ToList();
            //Return the filled article comment details list
            return articleCommentDetails;
        }
        #endregion
    }
}
